package utils;

import book.Book;
import book.PariOfUidAndBookId;
import com.bit.utils.FileUtils;

import java.io.IOException;
import java.time.LocalDate;

public class AnalyzingBook {

    //将书籍信息存放到指定文件中
    public void storeBook(Book[] book, String fileName) throws IOException {
        int useBookLength = 0;
        //统计实际使用的空间
        for (int i = 0; i < book.length; i++) {
            if(book[i] != null) useBookLength++;
        }
        //拼接
        StringBuilder toJson =new StringBuilder();
        for (int i = 0; i < useBookLength; i++) {
            if(book[i] != null){
                toJson.append(book[i].toJSON());
            }
            if(i != useBookLength - 1) toJson.append("\n");
        }
        //写入指定文件
        FileUtils.writeFile(toJson.toString(),fileName);
    }

    public static void main(String[] args) throws IOException{
        Book[] books = new Book[4];
        books[0] = new Book("C语言程序设计","laoHU","编程",1997, LocalDate.of(1997,8,21));
        books[1] = new Book("Java SE","laoHU","编程",1998, LocalDate.of(1998,8,21));
        books[2] = new Book("Java数据结构","laoHU","编程",1999, LocalDate.of(1999,8,21));
        books[3] = new Book("C语言程序设计","laoHU","编程",2000, LocalDate.of(2000,8,21));
        AnalyzingBook analyzingBook = new AnalyzingBook();
//        2,Java SE,laoHU,编程,1998,false,0,1998-08-21
//        3,Java数据结构,laoHU,编程,1999,false,0,1999-08-21
        analyzingBook.storeBook(books,"allbook.txt");

        Book[] book = analyzingBook.loadObject("allbook.txt");
        for (int i = 0; i < book.length; i++) {
            System.out.println(book[i]);
        }
    }

    //从指定文件中读取书籍信息
    public Book[] loadObject(String fileName) throws IOException{
        //1,从文件中读取内容
        String context = FileUtils.readFile("allbook.txt");
        if(context == null || context.isEmpty())
        {
            System.out.println("文件为空或者引用为空不可读取:" + fileName);
            return null;
        }
        //2.分割
        String[] booklist = context.split("\n");
        //3.将字符串组成书籍对象
        Book[] book = new Book[booklist.length];
        for (int i = 0; i < book.length;i++) {
            book[i] = parseBookJson(booklist[i]);
        }
        return book;
    }

    //序列号书籍信息
    private Book parseBookJson(String json){
        //1,判断
        if(json == null || json.isEmpty()){
            System.out.println("引用为空！！！");
            return null;
        }
        //2，分割
        String[] pairs = json.split(",");
        //3.赋值临时变量
        int boolID = Integer.parseInt(pairs[0]);
        String title = pairs[1];
        String auther = pairs[2];
        String  category= pairs[3];
        int publish = Integer.parseInt(pairs[4]);
        boolean isBorrowed = Boolean.parseBoolean(pairs[5]);
        int borrowCount = Integer.parseInt(pairs[6]);
        LocalDate shelfTime = LocalDate.parse(pairs[7]);
        //4.赋值对象
        if(title != null && auther != null && category != null && shelfTime != null) {
            Book book = new Book(title, auther, category, publish, shelfTime);
            book.setBookId(boolID);
            book.setBorrowed(isBorrowed);
            book.setBorrowCount(borrowCount);
            return book;
        }
        //5.返回
        return null;
    }

}
